package com.northpool.operator.cicd.notice;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@OperatorInfo(
        ref = "MailSendOperator",
        name = "邮件通知",
        description = "发送邮件，支持本地文件附件",
        version = "localFile"
)
public class MailSendOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "mail", name = "邮件地址", desc = "邮件地址, 如：test@piesat.cn", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "subject", name = "邮件主题", desc = "邮件地址, 如: test", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "content", name = "邮件内容", desc = "邮件内容, 如：hello", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "host", name = "邮箱服务器地址", desc = "邮箱服务器地址, 如：smtp.163.com", type = TypeEnum.STRING, value = "smtp.163.com"),
            @InputParam(ref = "from", name = "发送人邮箱", desc = "发送人邮箱, 如：northpool_work@163.com", type = TypeEnum.STRING, value = "northpool_work@163.com"),
            @InputParam(ref = "user", name = "发送人账号", desc = "发送人账号, 如：northpool_work", type = TypeEnum.STRING, value = "northpool_work"),
            @InputParam(ref = "pass", name = "发送人密码", desc = "发送人密码, 如：EBTbaXgUxGMWj7e5", type = TypeEnum.STRING, value = "EBTbaXgUxGMWj7e5"),
            @InputParam(ref = "files", name = "附件", desc = "附件路径,多个地址英文逗号隔开, 如：key1.txt", type = TypeEnum.STRING)
    })
    public MailSendOperator() {
    }

    public static void main(String[] args) {
        OperatorTestBuild.execute(MailSendOperator.class, TestParamBuilder.aBuilder()
                .param("mail", "1970594996@qq.com")
                .param("subject", "测试")
                .param("content", "hello")
                .param("host", "smtp.163.com")
                .param("from", "northpool_work@163.com")
                .param("user", "northpool_work")
                .param("pass", "EBTbaXgUxGMWj7e5")
                .param("files", "C:/key1.txt")
                .build()
        );
    }

    @Override
    public void execute(ParamTool paramTool) {
        MailAccount account = new MailAccount();
        account.setHost(paramTool.getStr("host"));
        account.setPort(25);
        account.setAuth(true);
        account.setFrom(paramTool.getStr("from"));
        account.setUser(paramTool.getStr("user"));
        account.setPass(paramTool.getStr("pass"));

        String mail = paramTool.getStr("mail");
        String subject = paramTool.getStr("subject");
        String content = paramTool.getStr("content");
        String files = paramTool.getStr("files");
        List<File> fileList = new ArrayList<>();
        if (StrUtil.isNotBlank(files)) {
            List<String> split = StrUtil.split(files, ",");
            for (String path : split) {
                if (!FileUtil.exist(path)) {
                    this.error("文件不存在: {}", path);
                    continue;
                }
                fileList.add(FileUtil.file(path));
            }
        }

        MailUtil.send(account,
                CollUtil.newArrayList(mail), subject, content, false,
                fileList.toArray(new File[0]));

        this.log("发送成功: {}", mail);
    }
}
